'use strict';
const Service = require('egg').Service;

class NewsController extends Service {
  async getnewslist (data) {
    console.log('获取新闻：', data);
    let sql = 'select * from news';
    const content = [];// 参数
    let isMore = false;// 是否有多个查询参数,只用于搜索查询
    if (data.news_id) {
      sql += ' where news_id = ?';
      content.push(data.news_id);
    }
    if (data.news_idORtitle) {
      sql += ' where concat(news_id,title) like ?';
      content.push('%' + data.news_idORtitle + '%');
      isMore = true;
    }
    // if (data.product_type) {
    //   if (isMore) { // true代表有多个参数
    //     sql += 'and product_type LIKE ?';// and是两个条件都必须满足，or是或的关系
    //   } else {
    //     sql += ' WHERE product_type LIKE ?';
    //   }
    //   content.push('%' + data.product_type + '%');
    //   isMore = true;
    // }
    if (data.begin && data.end) {
      if (isMore) { // true代表有多个参数
        sql += 'and publish_time between ? and ?';// and是两个条件都必须满足，or是或的关系
      } else {
        sql += ' WHERE publish_time between ? and ?';
      }
      content.push(data.begin, data.end);
      isMore = true;
    }
    // 开启分页
    if (data.current && data.pageSize) {
      const current = data.current;// 当前页码
      const pageSize = data.pageSize;// 一页展示多少条数据
      sql += ' LIMIT ?,?';
      content.push(parseInt((current - 1) * pageSize), parseInt(pageSize));
    }
    const res = {
      code: 200,
      page: data.current,
      pageSize: data.pageSize,
      data: [],
      total: '',
    };
    res.data = await this.app.mysql.query(
      sql, content
    );
    res.total = await this.app.mysql.count('news');
    return res;
  }
  async addlike (data) {
    console.log('添加新闻收藏：', data);
    const res = await this.app.mysql.insert('likes', data);
    return res;
  }
  async addnew (data) {
    console.log('添加新闻：', data);
    const res = await this.app.mysql.insert('news', data);
    return res;
  }
  async updatenew (data) {
    console.log('修改新闻：', data);
    const where = {
      where: {
        news_id: data.news_id,
      },
    };
    await this.app.mysql.update('news', data, where);
    return {
      code: 200,
      message: '新闻修改成功',
    };
  }
  async dellike (data) {
    console.log('取消新闻收藏', data);
    const where = {
      users_id: data.users_id,
      news_id: data.news_id,
    };
    const res = await this.app.mysql.delete('likes', where);
    return res;
  }
  async adminDellike (data) {
    console.log('取消收藏', data);
    const where = {
      likes_id: data.likes_id,
    };
    const res = await this.app.mysql.delete('likes', where);
    return res;
  }
  async delnew (data) {
    console.log('删除新闻', data);
    const where = {
      news_id: data.news_id,
    };
    const res = await this.app.mysql.delete('news', where);
    return res;
  }
  async isChecked (data) {
    console.log('获取收藏图标状态', data);
    const users_id = data.users_id;
    const news_id = data.news_id;
    const res = await this.app.mysql.query('select * from likes where users_id=? and news_id=?', [users_id, news_id]);
    return res;
  }
  async getmylike (data) {
    console.log('获取用户收藏：', data);
    const sql = 'select * from news left join likes on news.news_id = likes.news_id where likes.users_id = ?';
    const res = await this.app.mysql.query(sql, data.users_id);
    return res;
  }
  async getallnewlike (data) {
    console.log('获取所有新闻收藏：', data);
    // const sql = 'select * from news left join likes on news.news_id = likes.news_id';
    let sql = 'SELECT news.*,likes.* FROM news, likes where news.news_id = likes.news_id';
    const content = [];// 参数
    if (data.news_id) {
      sql += ' and likes.news_id = ?';
      content.push(data.news_id);
    }
    if (data.users_name) {
      sql += ' and likes.users_name like ?';// and是两个条件都必须满足，or是或的关系
      content.push(data.users_name);
    }
    if (data.begin && data.end) {
      sql += ' and create_time between ? and ?';// and是两个条件都必须满足，or是或的关系
      content.push(data.begin, data.end);
    }
    // 开启分页
    if (data.current && data.pageSize) {
      const current = data.current;// 当前页码
      const pageSize = data.pageSize;// 一页展示多少条数据
      sql += ' LIMIT ?,?';
      content.push(parseInt((current - 1) * pageSize), parseInt(pageSize));
    }
    const res = {
      code: 200,
      page: data.current,
      pageSize: data.pageSize,
      data: [],
      total: '',
    };
    res.data = await this.app.mysql.query(
      sql, content
    );
    const alllist = await this.app.mysql.query('SELECT news.*,likes.* FROM news, likes where news.news_id = likes.news_id');
    res.total = alllist.length;
    return res;
  }
  async getallproductlike (data) {
    console.log('获取所有舱位收藏：', data);
    let sql = 'SELECT products.*,likes.* FROM products, likes where products.product_id = likes.product_id';
    const content = [];// 参数
    if (data.product_id) {
      sql += ' and likes.product_id = ?';
      content.push(data.product_id);
    }
    if (data.users_name) {
      sql += ' and likes.users_name = ?';// and是两个条件都必须满足，or是或的关系
      content.push(data.users_name);
    }
    if (data.begin && data.end) {
      sql += ' and create_time >= ? and create_time <= ?';// and是两个条件都必须满足，or是或的关系
      content.push(data.begin, data.end);
    }
    // 开启分页
    if (data.current && data.pageSize) {
      const current = data.current;// 当前页码
      const pageSize = data.pageSize;// 一页展示多少条数据
      sql += ' LIMIT ?,?';
      content.push(parseInt((current - 1) * pageSize), parseInt(pageSize));
    }
    const res = {
      code: 200,
      page: data.current,
      pageSize: data.pageSize,
      data: [],
      total: '',
    };
    res.data = await this.app.mysql.query(
      sql, content
    );
    const alllist = await this.app.mysql.query('SELECT products.*,likes.* FROM products, likes where products.product_id = likes.product_id');
    res.total = alllist.length;
    return res;
  }
}
module.exports = NewsController;
